﻿#include "GUILoadConstraintDialog.h"
#include "ui_GUILoadConstraintDialog.h"
#include "FITK_Component/FITKAcousticsModel/FITKAbstractSEASubSys.h"

#include "FITK_Component/FITKAcousticsPhysics/FITKLoadConstraintFESubsys.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKLoadConstraintSEAAcoustic.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKLoadConstraintSEAShell.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKLoadConstraintSEABeam.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsAbstractSpectra.h"

namespace GUI {
    GUILoadConstraintDialog::GUILoadConstraintDialog(Core::FITKActionOperator* oper, QWidget* parent /*= nullptr*/) :
        GUILoadBaseDialog(nullptr, oper, parent),
        _ui(new Ui::GUILoadConstraintDialog)
    {
        _ui->setupUi(this);
        //创建新载荷数据
        this->initWidgetImage();
        this->createNewLoadConstraintObjData();
        this->init();
    }

    GUILoadConstraintDialog::GUILoadConstraintDialog(Acoustics::FITKAcousticsAbstractLoad* obj, Core::FITKActionOperator* oper, QWidget* parent /*= nullptr*/) :
        GUILoadBaseDialog(obj, oper, parent),
        _ui(new Ui::GUILoadConstraintDialog)
    {
        _ui->setupUi(this);
        //初始化 
        this->initWidgetImage();
        this->init();
    }

    GUILoadConstraintDialog::~GUILoadConstraintDialog()
    {
        //释放数据
        if (_ui) {
            delete _ui;
            _ui = nullptr;
        }
    }

    void GUILoadConstraintDialog::init()
    {
        _ui->tabWidget_Spectra->tabBar()->hide();
        if (!_obj) return;
        //初始化数据
        QString name = _obj->getDataObjectName();
        if (name.isEmpty())
        {
            name = "Constraint-1";
            name = this->ckeckNameFromMgr(name);
        }
        QString connectionName = _obj->getConnectionObjName();
        bool isEnabledState = _obj->getEnabledState();
        _ui->lineEdit_Name->setText(name);
        _ui->lineEdit_Connection->setText(connectionName);
        _ui->checkBox_EnabledState->setCheckState((isEnabledState ? Qt::CheckState::Checked : Qt::CheckState::Unchecked));
        //初始化相应的类型数据
        Acoustics::FITKAcousticsAbstractLoad::AcousticsLoadType type = _obj->getAcousticsLoadType();
        if (type == Acoustics::FITKAcousticsAbstractLoad::AcousticsLoadType::ConstraintFESubsys)
            this->initConstraintFESubsysData();
        else if (type == Acoustics::FITKAcousticsAbstractLoad::AcousticsLoadType::ConstraintSEAAcoustic)
            this->initConstraintSEAAcousticData();
        else if (type == Acoustics::FITKAcousticsAbstractLoad::AcousticsLoadType::ConstraintSEAShell)
            this->initConstraintSEAShellData();
        else if (type == Acoustics::FITKAcousticsAbstractLoad::AcousticsLoadType::ConstraintSEABeam)
            this->initConstraintSEABeamData();
    }

    void GUILoadConstraintDialog::on_pushButton_Cancel_clicked()
    {
        //点击取消按键动作
        this->reject();
    }

    void GUILoadConstraintDialog::on_pushButton_OK_clicked()
    {
        if (!_oper)return;
        //获取操作器动作类型
        QString operType;
        _oper->argValue<QString>("OperModelType", operType);
        if (operType.isEmpty())
        {
            delete _obj;
            this->reject();
            return;
        }
        //创建动作
        if (operType == "Create")
        {
            if (!this->createObjData())
                return;
        }
        //修改动作
        else if (operType == "Edit")
        {
            if (!this->editObjData())
                return;
        }
        this->accept();
    }

    void GUILoadConstraintDialog::initWidgetImage()
    {
        //初始化图像
        _ui->label->setPixmap(QPixmap(":/ToolBar/toolBar/load_Constraint.png"));
    }

    void GUILoadConstraintDialog::initConstraintFESubsysData()
    {
        _ui->tabWidget_Spectra->setCurrentIndex(3);
        Acoustics::FITKLoadConstraintFESubsys* loadData = dynamic_cast<Acoustics::FITKLoadConstraintFESubsys*>(_obj);
        if (!loadData) return;
    }

    void GUILoadConstraintDialog::initConstraintSEAAcousticData()
    {
        _ui->tabWidget_Spectra->setCurrentIndex(2);
        //初始化下拉数据
        QList<QComboBox*> comboBoxList;
        QList<int> typeList;
        comboBoxList << _ui->comboBox_AcouConstraint_Spectra;
        typeList << Acoustics::FITKAcousticsAbstractSpectra::AcousticsSpectraType::Energy << Acoustics::FITKAcousticsAbstractSpectra::AcousticsSpectraType::Pressure;
        this->setComboBoxSpectra(comboBoxList, typeList);
        //初始化数据
        Acoustics::FITKLoadConstraintSEAAcoustic* loadData = dynamic_cast<Acoustics::FITKLoadConstraintSEAAcoustic*>(_obj);
        if (!loadData) return;
        int spectraID = loadData->getSpectraDataID();
        int index = _ui->comboBox_AcouConstraint_Spectra->findData(spectraID);
        _ui->comboBox_AcouConstraint_Spectra->setCurrentIndex((index < 0 ? 0 : index));
    }

    void GUILoadConstraintDialog::initConstraintSEAShellData()
    {
        _ui->tabWidget_Spectra->setCurrentIndex(1);
        //初始化下拉数据
        QList<QComboBox*> comboBoxList;
        QList<int> typeList;
        comboBoxList << _ui->comboBox_Extension << _ui->comboBox_Shear << _ui->comboBox_Flexure;
        typeList << Acoustics::FITKAcousticsAbstractSpectra::AcousticsSpectraType::Acceleration << Acoustics::FITKAcousticsAbstractSpectra::AcousticsSpectraType::Energy << Acoustics::FITKAcousticsAbstractSpectra::AcousticsSpectraType::Velocity;
        this->setComboBoxSpectra(comboBoxList, typeList);
        //初始化数据
        Acoustics::FITKLoadConstraintSEAShell* loadData = dynamic_cast<Acoustics::FITKLoadConstraintSEAShell*>(_obj);
        if (!loadData) return;
        int extensionID = loadData->getExtensionSpectraID();
        int shearID = loadData->getShearSpectraID();
        int flexureID = loadData->getFlexureSpectraID();
        int index = _ui->comboBox_Extension->findData(extensionID);
        _ui->comboBox_Extension->setCurrentIndex((index < 0 ? 0 : index));
        index = _ui->comboBox_Shear->findData(shearID);
        _ui->comboBox_Shear->setCurrentIndex((index < 0 ? 0 : index));
        index = _ui->comboBox_Flexure->findData(flexureID);
        _ui->comboBox_Flexure->setCurrentIndex((index < 0 ? 0 : index));
    }

    void GUILoadConstraintDialog::initConstraintSEABeamData()
    {
        _ui->tabWidget_Spectra->setCurrentIndex(0);
        //初始化下拉数据
        QList<QComboBox*> comboBoxList;
        QList<int> typeList;
        comboBoxList << _ui->comboBox_Flexural_X << _ui->comboBox_Flexural_Y << _ui->comboBox_Extension_Z;
        typeList << Acoustics::FITKAcousticsAbstractSpectra::AcousticsSpectraType::Acceleration << Acoustics::FITKAcousticsAbstractSpectra::AcousticsSpectraType::Energy << Acoustics::FITKAcousticsAbstractSpectra::AcousticsSpectraType::Velocity;
        this->setComboBoxSpectra(comboBoxList, typeList);
        this->setComboBoxSpectra(QList<QComboBox*>() << _ui->comboBox_Torsional_ZZ, QList<int>() << Acoustics::FITKAcousticsAbstractSpectra::AcousticsSpectraType::AnlrVelocity << Acoustics::FITKAcousticsAbstractSpectra::AcousticsSpectraType::Energy);
        //初始化数据
        Acoustics::FITKLoadConstraintSEABeam* loadData = dynamic_cast<Acoustics::FITKLoadConstraintSEABeam*>(_obj);
        if (!loadData) return;
        int flexureXID = loadData->getFlexureXSpectraID();
        int flexureYID = loadData->getFlexureYSpectraID();
        int torsionalZZID = loadData->getTorsionZZSpectraID();
        int extensionZID = loadData->getExtensionZSpectraID();
        int index = _ui->comboBox_Flexural_X->findData(flexureXID);
        _ui->comboBox_Flexural_X->setCurrentIndex((index < 0 ? 0 : index));
        index = _ui->comboBox_Flexural_Y->findData(flexureYID);
        _ui->comboBox_Flexural_Y->setCurrentIndex((index < 0 ? 0 : index));
        index = _ui->comboBox_Torsional_ZZ->findData(torsionalZZID);
        _ui->comboBox_Torsional_ZZ->setCurrentIndex((index < 0 ? 0 : index));
        index = _ui->comboBox_Extension_Z->findData(extensionZID);
        _ui->comboBox_Extension_Z->setCurrentIndex((index < 0 ? 0 : index));
    }

    void GUILoadConstraintDialog::createNewLoadConstraintObjData()
    {
        //获取拾取数据
        if (!_oper) return;
        int pickObjID = 0;
        if (!_oper->argValue<int>("PickObjID", pickObjID)) return;
        double connectionLocation[3]{ 0.0,0.0,0.0 };
        if (!_oper->argValue<double>("connectPointX", connectionLocation[0])) return;
        if (!_oper->argValue<double>("connectPointY", connectionLocation[1])) return;
        if (!_oper->argValue<double>("connectPointZ", connectionLocation[2])) return;
        //获取拾取类型
        bool isSeaModel = false;
        int seaType = 0;
        if (!this->getPickDiscDataType(pickObjID, isSeaModel, seaType)) return;
        //创建新的数据
        if (isSeaModel == false)
            _obj = new Acoustics::FITKLoadConstraintFESubsys;
        else if (isSeaModel && seaType == Acoustics::FITKAbstractSEASubSys::SEASubSysType::SEACavity)
            _obj = new Acoustics::FITKLoadConstraintSEAAcoustic;
        else if (isSeaModel && (seaType == Acoustics::FITKAbstractSEASubSys::SEASubSysType::SEACylinderShell || seaType == Acoustics::FITKAbstractSEASubSys::SEASubSysType::SEADoublyCurvedShell ||
            seaType == Acoustics::FITKAbstractSEASubSys::SEASubSysType::SEAFlatPlate || seaType == Acoustics::FITKAbstractSEASubSys::SEASubSysType::SEASinglyCurvedShell))
            _obj = new Acoustics::FITKLoadConstraintSEAShell;
        else if (isSeaModel && seaType == Acoustics::FITKAbstractSEASubSys::SEASubSysType::SEABeam)
            _obj = new Acoustics::FITKLoadConstraintSEABeam;
        if (!_obj) return;
        //设置连接器对象
        _obj->setConnectionObjID(pickObjID);
        _obj->setConnectionLocation(connectionLocation);
    }

    void GUILoadConstraintDialog::getDataFormWidget()
    {
        if (!_obj) return;
        bool isEnabledState = _ui->checkBox_EnabledState->checkState() == Qt::CheckState::Checked;
        _obj->setEnabledState(isEnabledState);
        //从界面获取相应的类型数据
        Acoustics::FITKAcousticsAbstractLoad::AcousticsLoadType type = _obj->getAcousticsLoadType();
        if (type == Acoustics::FITKAcousticsAbstractLoad::AcousticsLoadType::ConstraintFESubsys)
            this->getConstraintFESubsysDataFormWidget();
        else if (type == Acoustics::FITKAcousticsAbstractLoad::AcousticsLoadType::ConstraintSEAAcoustic)
            this->getConstraintSEAAcousticDataFormWidget();
        else if (type == Acoustics::FITKAcousticsAbstractLoad::AcousticsLoadType::ConstraintSEAShell)
            this->getConstraintSEAShellDataFormWidget();
        else if (type == Acoustics::FITKAcousticsAbstractLoad::AcousticsLoadType::ConstraintSEABeam)
            this->getConstraintSEABeamDataFormWidget();
    }

    void GUILoadConstraintDialog::getConstraintFESubsysDataFormWidget()
    {
        Acoustics::FITKLoadConstraintFESubsys* loadData = dynamic_cast<Acoustics::FITKLoadConstraintFESubsys*>(_obj);
        if (!loadData) return;
    }

    void GUILoadConstraintDialog::getConstraintSEAAcousticDataFormWidget()
    {
        Acoustics::FITKLoadConstraintSEAAcoustic* loadData = dynamic_cast<Acoustics::FITKLoadConstraintSEAAcoustic*>(_obj);
        if (!loadData) return;
        //从窗口获取数据
        int spectraID = _ui->comboBox_AcouConstraint_Spectra->currentData().toInt();
        //设置数据
        loadData->setSpectraDataID(spectraID);
    }

    void GUILoadConstraintDialog::getConstraintSEAShellDataFormWidget()
    {
        Acoustics::FITKLoadConstraintSEAShell* loadData = dynamic_cast<Acoustics::FITKLoadConstraintSEAShell*>(_obj);
        if (!loadData) return;
        //从窗口获取数据
        int extensionID = _ui->comboBox_Extension->currentData().toInt();
        int shearID = _ui->comboBox_Shear->currentData().toInt();
        int flexureID = _ui->comboBox_Flexure->currentData().toInt();
        //设置数据
        loadData->setExtensionSpectraID(extensionID);
        loadData->setShearSpectraID(shearID);
        loadData->setFlexureSpectraID(flexureID);
    }

    void GUILoadConstraintDialog::getConstraintSEABeamDataFormWidget()
    {
        Acoustics::FITKLoadConstraintSEABeam* loadData = dynamic_cast<Acoustics::FITKLoadConstraintSEABeam*>(_obj);
        if (!loadData) return;
        //从窗口获取数据
        int flexureXID = _ui->comboBox_Flexural_X->currentData().toInt();
        int flexureYID = _ui->comboBox_Flexural_Y->currentData().toInt();
        int torsionalZZID = _ui->comboBox_Torsional_ZZ->currentData().toInt();
        int extensionZID = _ui->comboBox_Extension_Z->currentData().toInt();
        //设置数据
        loadData->setFlexureXSpectraID(flexureXID);
        loadData->setFlexureYSpectraID(flexureYID);
        loadData->setTorsionZZSpectraID(torsionalZZID);
        loadData->setExtensionZSpectraID(extensionZID);
    }

    QString GUILoadConstraintDialog::getNameFormWidget()
    {
        return _ui->lineEdit_Name->text();
    }
}
